<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>模板代码生成</title>
    <link rel="stylesheet" href="/css/ry-ui.css">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/layui-v2.5.5/layui/css/layui.css">
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="gen-form">
                <div class="select-list">
                    <ul>
                        <li>
                            表名称：<input type="text" name="tableName" id="tableName"/>
                        </li>
                        <li>
                            表描述：<input type="text" name="tableComment" id="tableComment"/>
                        </li>
                        <li class="select-time">
                            <label>表时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>

                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" data-type="reload" id="search"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>


                </div>
            </form>
            <form id="code" style="float: left">
                <ul>
                    <li>
                        代码风格：<select id="codeStyle" name="codeStyle">
                        <option value="ffcs" selected>消防</option>
                        <option value="mybatisplus">ruoyi-mybatisplus</option>
                        <option value="raw">ruoyi</option>
                        </select>
                    </li>
                    <li>
                        java包名：<input type="text" name="javaPackage" id="javaPackage" />
                    </li>
                    <li>
                        java路径：<input type="text" name="javaPath" id="javaPath" />
                    </li>
                    <li>
                        模块名：<input type="text" name="moduleName" id="moduleName"  />
                    </li>

                </ul>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="layui-table" lay-filter="demo" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="gen">生成</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchGen">批量生成</button>
    </div>
</script>

<script th:src="@{/layui-v2.5.5/layui/layui.js}"></script>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/common.js}"></script>
<!--<script th:src="@{/js/bootstrap.min.js}"></script>-->

<!--<script th:src="@{/index.js}" src="index.js"></script>-->

<script th:inline="javascript">
    var ctx = [[@{/}]];
</script>

<script type="text/javascript" th:inline="none">
    var prefix = ctx + "tool/gen";

    layui.use(['laydate', 'table'], function () {
        var table = layui.table;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime' //指定元素
            , theme: '#79be00'
            , position: 'fixed'
        });
        laydate.render({
            elem: '#endTime' //指定元素
            , theme: '#79be00'
            , position: 'fixed'
        });

        table.render({
            elem: '#layui-table'
            , height: 'full-150'
            , url: prefix + "/list"
            , method: "post"
            , toolbar: '#toolbarDemo'
            , id: 'testReload'
            , page: true,
            cols: [[//thymeleaf 和layui [[冲突,加空格 [ [
                {checkbox: true, fixed: true},
                {field: 'tableName', title: '表名称', width: 250, sort: true},
                {field: 'tableComment', title: '表描述', width: 250},
                {field: 'createTime', title: '创建时间', width: 180},
                {field: 'updateTime', title: '更新时间', width: 180},
                {
                    field: 'operation',
                    fixed: 'right',
                    title: '操作',
                    align: 'center',
                    width: 250,
                    toolbar: '#barDemo'
                }
            ]]
        });

        //监听工具条
        table.on('tool(demo)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data//获得当前行数
                , layEvent = obj.event; //获得 lay-event 对应的值
           let codeStyle =  $("#codeStyle option:selected").val();
            if (layEvent === 'gen') {
                layer.confirm("确定要生成" + data.tableName + "表代码吗？", function (index) {
                    let codeStyle =  $("#codeStyle option:selected").val();
                    let moduleName = $("#moduleName").val();
                    let javaPath = $("#javaPath").val();
                    let javaPackage = $("#javaPackage").val();
                    location.href = prefix + "/genCode/" + data.tableName+"?style="+codeStyle+"&moduleName="+moduleName+"&javaPath="+javaPath+"&javaPackage="+javaPackage;
                    layer.msg('执行成功,正在生成代码请稍后…', {icon: 1});
                    layer.close(index);
                });
            }

        });

        //工具栏事件
        table.on('toolbar(demo)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchGen':
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg('请先选择要生成的数据行！', {icon: 2});
                        return;
                    }
                    layer.confirm("确认要生成选中的" + data.length + "条数据吗?", function (index) {
                        var ids = "";
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].tableName + ",";
                        }
                        layer.msg(ids);
                        let codeStyle =  $("#codeStyle option:selected").val();
                        let moduleName = $("#moduleName").val();
                        let javaPath = $("#javaPath").val();
                        let javaPackage = $("#javaPackage").val();
                        location.href = prefix + "/batchGenCode?tables=" + ids+"&style="+codeStyle+"&moduleName="+moduleName+"&javaPath="+javaPath+"&javaPackage="+javaPackage;
                        layer.msg('执行成功,正在生成代码请稍后…', {icon: 1});
                    });
                    break;
            }
            ;
        });

        var $ = layui.$, active = {
            reload: function () {
                var tableName = $('#tableName').val();
                var tableComment = $('#tableComment').val();
                var startTime = $('#startTime').val();
                var endTime = $('#endTime').val();
                var params = {
                    beginTime: startTime,
                    endTime: endTime
                };
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {     //where内的内容进行传递
                        tableName: tableName,
                        tableComment: tableComment,
                        params: params,

                    }
                });
            }

        };
        $('#search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
</body>
</html>